//
//  RCTicketAddUserViewController.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-11-30.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCTicketAddUserViewController.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "NSArray+EX.h"
#import "AppDelegate.h"
#import "NSArray+EX.h"
#import "NSString+NSString_Ex_.h"


#define kHeaderLineImageFile             @"image_ticketOrder_addUser_line.png"
#define kBGImageFile          @"image_ticketOrder_addUser_bg.png"
#define kCommitImageFile      @"image_ticketOrder_addUser_commit.png"
#define kLineImageFile          @"image_line_type3.png"
#define kArrowImageFile         @"flyTicket_orderCell_arrow.png"

@interface RCTicketAddUserViewController ()<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>

@property(nonatomic,retain) UIButton *commitButton;
@property(nonatomic,retain) UITextField *nameTextField;
@property(nonatomic,retain) UITextField *numberTextField;
@property(nonatomic,retain) UIView *numberTypeToolBar;
@property(nonatomic,retain) UIPickerView *numberTypePickerView;
@property(nonatomic,retain) UILabel *numberTypeLabel;
@property(nonatomic,retain) NSMutableArray *numberTypeArray;
@property(nonatomic,copy)   NSString *numberTypeString;


@end

@implementation RCTicketAddUserViewController
@synthesize commitButton = _commitButton;
@synthesize nameTextField = _nameTextField;
@synthesize numberTextField = _numberTextField;
@synthesize numberTypeToolBar = _numberTypeToolBar;
@synthesize numberTypePickerView = _numberTypePickerView;
@synthesize numberTypeLabel = _numberTypeLabel;
@synthesize numberTypeArray = _numberTypeArray;
@synthesize numberTypeString = _numberTypeString;
@synthesize delegate = _delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)dealloc{
    
    self.delegate = nil;
    self.commitButton = nil;
    self.nameTextField = nil;
    self.numberTextField = nil;
    self.numberTypeToolBar = nil;
    self.numberTypePickerView = nil;
    self.numberTypeLabel = nil;
    self.numberTypeArray = nil;
    self.numberTypeString = nil;
    [super dealloc];
}

-(void)loadView{
    [super loadView];
    self.titleLabelText = @"新增乘客";
    self.navigationItem.titleView = [self createTitleView];
    if (!_commitButton) {
        _commitButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 0, 44, 44)];
        _commitButton.backgroundColor = [UIColor clearColor];
        NSString *sharePath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:kCommitImageFile];
        [_commitButton setImage:[UIImage imageWithContentsOfFile:sharePath] forState:UIControlStateNormal];
        [_commitButton setImage:[UIImage imageWithContentsOfFile:sharePath] forState:UIControlStateSelected];
        [_commitButton addTarget:self action:@selector(commitButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.navigationItem.rightBarButtonItem = [self createRightBarButtonItemWithButton:_commitButton];
    self.navigationItem.leftBarButtonItem = [self createBackBarButtonItem];
    
    _numberTypeArray = [[NSMutableArray alloc]initWithObjects:@"身份证", @"护照",@"港澳通行证",@"台湾通行证",nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]];
    
    UIImageView *nameImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 14, 291, 38)];
    nameImageView.userInteractionEnabled = YES;
    nameImageView.backgroundColor = [UIColor clearColor];
    [nameImageView setImage:bgImage];
    
    UIImageView *nameStarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 15, 7, 8)];
    nameStarImageView.backgroundColor = [UIColor clearColor];
    [nameStarImageView setImage:[UIImage imageNamed:@"icon_ticketOrder_star"]];
    [nameImageView addSubview:nameStarImageView];
    [nameStarImageView release];
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 12, 100, 14)];
    nameLabel.backgroundColor = [UIColor clearColor];
    nameLabel.textColor = kNormalTextColor;
    nameLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    nameLabel.text = @"中文姓名";
    [nameImageView addSubview:nameLabel];
    [nameLabel release];
    
    _nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(110, 0, 196, 38)];
    _nameTextField.backgroundColor = [UIColor clearColor];
    _nameTextField.delegate = self;
    _nameTextField.textColor = kNormalTextColor;
    _nameTextField.font = [UIFont fontWithName:kFontNameHelvetica size:14];
    _nameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _nameTextField.placeholder = @"所选证件姓名";
    [_nameTextField setReturnKeyType:UIReturnKeyDone];
    [nameImageView addSubview:_nameTextField];
    
    UIImageView *nameLineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 37, 320, 1)];
    nameLineImageView.backgroundColor = [UIColor clearColor];
    [nameLineImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kLineImageFile ofType:nil]]];
    [nameImageView addSubview:nameLineImageView];
    [nameLineImageView release];
    
    [self.viewContainer addSubview:nameImageView];
    [nameImageView release];
    
    
    UIButton *numTypeImageView = [[UIButton alloc]initWithFrame:CGRectMake(14, 52, 291, 38)];
    numTypeImageView.backgroundColor = [UIColor clearColor];
    [numTypeImageView setBackgroundImage:bgImage forState:UIControlStateNormal];
    [numTypeImageView setBackgroundImage:bgImage forState:UIControlStateHighlighted];
    [numTypeImageView addTarget:self action:@selector(numberTypeButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *numTypeStarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 15, 7, 8)];
    numTypeStarImageView.backgroundColor = [UIColor clearColor];
    [numTypeStarImageView setImage:[UIImage imageNamed:@"icon_ticketOrder_star"]];
    [numTypeImageView addSubview:numTypeStarImageView];
    [numTypeStarImageView release];
    
    UILabel *numTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 12, 100, 14)];
    numTypeLabel.backgroundColor = [UIColor clearColor];
    numTypeLabel.textColor = kNormalTextColor;
    numTypeLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    numTypeLabel.text = @"证件类型";
    [numTypeImageView addSubview:numTypeLabel];
    [numTypeLabel release];
    
    _numberTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(117, 12, 100, 14)];
    _numberTypeLabel.backgroundColor = [UIColor clearColor];
    _numberTypeLabel.textColor = [UIColor colorWithHex:0xff505050];
    _numberTypeLabel.font = [UIFont fontWithName:kFontNameHeiti size:14];
    _numberTypeLabel.text = @"身份证";
    [numTypeImageView addSubview:_numberTypeLabel];
    
    UIImageView *arrowImageView = [[UIImageView alloc]initWithFrame:CGRectMake(280, 13, 10, 12)];
    arrowImageView.backgroundColor = [UIColor clearColor];
    [arrowImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kArrowImageFile ofType:nil]]];
    [numTypeImageView addSubview:arrowImageView];
    [arrowImageView release];
    
    UIImageView *numTypeLineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 37, 320, 1)];
    numTypeLineImageView.backgroundColor = [UIColor clearColor];
    [numTypeLineImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kLineImageFile ofType:nil]]];
    [numTypeImageView addSubview:numTypeLineImageView];
    [numTypeLineImageView release];
    
    [self.viewContainer addSubview:numTypeImageView];
    [numTypeImageView release];
    
    
    UIImageView *numberImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 90, 291, 38)];
    numberImageView.userInteractionEnabled = YES;
    numberImageView.backgroundColor = [UIColor clearColor];
    [numberImageView setImage:bgImage];
    
    UIImageView *numberStarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 15, 7, 8)];
    numberStarImageView.backgroundColor = [UIColor clearColor];
    [numberStarImageView setImage:[UIImage imageNamed:@"icon_ticketOrder_star"]];
    [numberImageView addSubview:numberStarImageView];
    [numberStarImageView release];
    
    UILabel *numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 12, 100, 14)];
    numberLabel.backgroundColor = [UIColor clearColor];
    numberLabel.textColor = kNormalTextColor;
    numberLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    numberLabel.text = @"证件号码";
    [numberImageView addSubview:numberLabel];
    [numberLabel release];
    
    _numberTextField = [[UITextField alloc]initWithFrame:CGRectMake(110, 0, 196, 38)];
    _numberTextField.backgroundColor = [UIColor clearColor];
    _numberTextField.delegate = self;
    _numberTextField.textColor = kNormalTextColor;
    _numberTextField.font = [UIFont fontWithName:kFontNameHelvetica size:14];
    _numberTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _numberTextField.placeholder = @"点击编辑证件号";
    [_numberTextField setReturnKeyType:UIReturnKeyDone];
    [numberImageView addSubview:_numberTextField];
    
    UIImageView *numberLineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 37, 320, 1)];
    numberLineImageView.backgroundColor = [UIColor clearColor];
    [numberLineImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kLineImageFile ofType:nil]]];
    [numberImageView addSubview:numberLineImageView];
    [numberLineImageView release];
    
    [self.viewContainer addSubview:numberImageView];
    [numberImageView release];
    
    UIImageView *headerImageLineView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 4)];
    headerImageLineView.backgroundColor = [UIColor clearColor];
    [headerImageLineView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kHeaderLineImageFile ofType:nil]]];
    [self.viewContainer addSubview:headerImageLineView];
    [headerImageLineView release];
    
    [self updatePickerView];
    
}

-(void)updatePickerView{
    _numberTypePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, UIScreenHeight-44-20-150, 320, 150)];
    _numberTypePickerView.showsSelectionIndicator = YES;
    _numberTypePickerView.backgroundColor = [UIColor whiteColor];
    
    [self.viewContainer addSubview:_numberTypePickerView];
    _numberTypePickerView.delegate = self;
    _numberTypePickerView.hidden = YES;
    
    _numberTypeToolBar = [self createToolBar];
    [self.viewContainer addSubview:_numberTypeToolBar];
    _numberTypeToolBar.hidden = YES;
}


- (UIView *) createToolBar
{
    CGFloat yPoint = 0;
    yPoint = _numberTypePickerView.frame.origin.y-40;
    
    UIView *s = [[[UIView alloc] initWithFrame:CGRectMake(0, yPoint, 320, 40)]autorelease];
    [s setBackgroundColor:kNormalOrangeTextColor];
    
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 50, 40)];
    [leftBtn addTarget:self action:@selector(cancelBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    [leftBtn.titleLabel setFont:[UIFont fontWithName:kFontNameHeiti size:16]];
    [leftBtn setTitleColor:kNormalHeadTextColor forState:UIControlStateNormal];
    [leftBtn setBackgroundColor:[UIColor clearColor]];
    
    [s addSubview:leftBtn];
    [leftBtn release];
    
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, 220, 40)];
    [s addSubview:textLabel];
    [textLabel setBackgroundColor:[UIColor clearColor]];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.text = @"选择证件类型";
    [textLabel release];
    
    
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(260, 0, 50, 40)];
    [s addSubview:rightBtn];
    [rightBtn addTarget:self action:@selector(confirmBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitle:@"确定" forState:UIControlStateNormal];
    [rightBtn.titleLabel setFont:[UIFont fontWithName:kFontNameHeiti size:16]];
    [rightBtn setTitleColor:kNormalHeadTextColor forState:UIControlStateNormal];
    [rightBtn setBackgroundColor:[UIColor clearColor]];
    [rightBtn release];
    return s;
    
}


#pragma mark PickView Delegate

-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    return [self.numberTypeArray count];
    
}


-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSString *t = nil;
    
    t = [self.numberTypeArray safeObjectAtIndex:row];
    return t;
}



- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *t = nil;
    
    t = [self.numberTypeArray safeObjectAtIndex:row];
    if (nil == t) {
        return;
    }
    
    self.numberTypeString = t;
    
}

- (void) cancelBtnPressed
{
    _numberTypeToolBar.hidden = YES;
    _numberTypePickerView.hidden = YES;
}


- (void) confirmBtnPressed
{
    _numberTypeToolBar.hidden = YES;
    _numberTypePickerView.hidden = YES;
    _numberTypeLabel.text = self.numberTypeString;
}

#pragma -mark UITextFeildDelegate

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}

-(void)commitButtonClicked{
    if ([NSString stringIsNullOrEmpty:_nameTextField.text]) {
        [RCAppDelegate alertWithCustomizeString:@"请填写姓名"];
        return;
    }
    
    if ([NSString stringIsNullOrEmpty:_numberTypeLabel.text]) {
        [RCAppDelegate alertWithCustomizeString:@"请选择证件类型"];
        return;
    }
    
    if ([NSString stringIsNullOrEmpty:_numberTextField.text]) {
        [RCAppDelegate alertWithCustomizeString:@"请填写证件号码"];
        return;
    }
    
    RCTicketOrderUserInfo *userInfo = [[RCTicketOrderUserInfo alloc]init];
    userInfo.userName = _nameTextField.text;
    userInfo.userNum = _numberTextField.text;
    userInfo.userNumType = _numberTypeLabel.text;
    if (_delegate &&[_delegate respondsToSelector:@selector(getUserDataModel:)]) {
        [_delegate getUserDataModel:userInfo];
    }
    [userInfo release];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void) backBarButtonItemPressed:(id) sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)numberTypeButtonClicked{
    _numberTypePickerView.hidden = NO;
    _numberTypeToolBar.hidden = NO;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
